package com.waterworld.watch.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.CourseBean;

import java.util.List;

/**
 * Created by nhuan
 * Time:2019/3/8.
 */

public class ClassTimeAdapter extends RecyclerView.Adapter<ClassTimeAdapter.MyHolder> {

    private Context mContext;
    private List<CourseBean.Time> mData;

    public ClassTimeAdapter(Context mContext, List<CourseBean.Time> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    public List<CourseBean.Time> getData() {
        return mData;
    }
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.item_class_time, viewGroup, false);
        itemView.getLayoutParams().height = viewGroup.getHeight()/8;
        return new ClassTimeAdapter.MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.tv_time_start.setText(mData.get(i).getStartTime()+"~");
        myHolder.tv_time_end.setText(mData.get(i).getEndTime());
        myHolder.tv_class_number.setText(i+1+"");
        if (listener != null) {
            myHolder.ll_class.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = myHolder.getLayoutPosition();
                    listener.onClick(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tv_time_start;
        private TextView tv_time_end;
        private TextView tv_class_number;
        private LinearLayout ll_class;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_time_start = itemView.findViewById(R.id.tv_time_start);
            tv_time_end = itemView.findViewById(R.id.tv_time_end);
            tv_class_number = itemView.findViewById(R.id.tv_class_number);
            ll_class = itemView.findViewById(R.id.ll_class);
        }
    }

    private ClassTimeAdapter.SetonClickListener listener;

    public void setOnItemClick(ClassTimeAdapter.SetonClickListener listener) {
        this.listener = listener;
    }

    public interface SetonClickListener {
        void onClick(View view, int position);
    }
}
